package org.gthelper.handler;

import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;

import org.gthelper.data.DataGeter;
import org.gthelper.error.ErrorCode;
import org.gthelper.imageProcess.ImageProcess;
import org.gthelper.imageProcess.ImageProcessFactory;
import org.gthelper.order.OrderInfo;


public class Process extends Handler
{

	@Override
	public int handle(OrderInfo orderinfo) {
		// TODO 自动生成的方法存根
		//get id
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		DataGeter dg = new DataGeter();
		int errcode=dg.getData(orderinfo.orderStream, baos);
		if(errcode!=ErrorCode.NORMAL)return errcode;
		byte[] userid = baos.toByteArray();
		baos.reset();
		
		//get filename
		errcode = dg.getData(orderinfo.orderStream, baos);
		if(errcode!=ErrorCode.NORMAL)return errcode;
		byte[] filename = baos.toByteArray();
		baos.reset();
		
		//get mode
		//get filename
		errcode = dg.getData(orderinfo.orderStream, baos);
		if(errcode!=ErrorCode.NORMAL)return errcode;
		byte[] mode = baos.toByteArray();
		baos.reset();
		
		//to String
		String usrid="";
		String fileName="";
		String processmode="";
		try {
			usrid = new String(userid,"UTF-8");
			fileName = new String(filename,"UTF-8");
			processmode = new String(mode,"UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
			return ErrorCode.UNKNOWN;
		}
		if((fileName.indexOf('/'))>-1)return ErrorCode.WRONGNAME;
		if((fileName.indexOf('\\'))>-1)return ErrorCode.WRONGNAME;
		ImageProcess iper = ImageProcessFactory.getProcesser(processmode);
		return iper.doProcess(orderinfo, usrid, fileName);
		
	}
	
}